package com.pajk.user.helper;

import com.pajk.idgen.IDGenService;
import com.pajk.user.service.common.idpool.MemIDPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by peide.zhang on 2014/5/24.
 */
public class RandomData {

    private static final Random random = new Random();
    private static Long idGenerator = 10000L;
    private static MemIDPool idPool;
    static{
        idPool = new MemIDPool("user-service", "user", new MockIdGenerator());
        idPool.setAllocCount(1000);
    }

    public static String getRandomPhoneNumber()
    {
        String phoneNumber;

        phoneNumber = "13" + random.nextInt(9) + random.nextInt(9)
               + random.nextInt(9) + random.nextInt(9) + random.nextInt(9) + random.nextInt(9)
                + random.nextInt(9) + random.nextInt(9) + random.nextInt(9);

        return phoneNumber;
    }
    public static Long borrowAndConsumeId(){
        String id=idPool.borrow();
        idPool.consume(id);
        return Long.valueOf(id);
    }
    private static class MockIdGenerator implements IDGenService {
        private final Logger Log = LoggerFactory.getLogger(MockIdGenerator.class);


        @Override
        public synchronized String getNextId(String s, String s2, int i) {
            List<Long> Ids = new ArrayList<Long>();
            for(int j = 0; j < i; j++){
                while (((idGenerator / 10) % 1000 != 0)){
                    idGenerator += 999;
                }
                Ids.add(idGenerator);
               idGenerator += 1;
            }

            StringBuilder sb = new StringBuilder();
            for(Long id : Ids){
                sb.append(id).append(',');
            }
            System.out.println(sb);
            String out = sb.substring(0, sb.length()-1);
            Log.debug("generated {} ids, raw string: {}", i, out);
            return out;
        }
    }

}
